package com.tellhow.monitorserverqh.controller;

import com.tellhow.monitorserverqh.model.Infos;
import com.tellhow.monitorserverqh.model.Servers;
import com.tellhow.monitorserverqh.service.IMonitorServer;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "sms")
//如果只有一个主配置类文件，@PropertySource可以不写
@PropertySource("classpath:application.properties")
@Data
@RestController
@RequestMapping(value = "/test")
public class Testcontroller {
    /**
     * 测试代码
     */
    @Autowired
    IMonitorServer iMonitorServer;
    //@Value("${com.tellhow.test}")
    private String retStr;
    @Value("${sms.info.groupid}")
    private String groupid;
    /**
     * data.person.name
     * 这里map名需要和application.properties中的参数一致
     */
    private Map<String, Object> info = new HashMap<>();
    @Autowired
    private ApplicationContext ioc;
    @Autowired
    private Servers servers;

    @RequestMapping("/test")
    public String Test() {
        return retStr;
    }

    @RequestMapping("/my")
    public String My() {

        String mapDetails = "";
        for (String key : info.keySet()
        ) {
            mapDetails = mapDetails + info.get(key);
        }
        return "读取map内容：" + info.toString() + mapDetails;
    }

    @RequestMapping("/pg")
    public String ProgramRunTest() {
        String result = "";
        try {
            iMonitorServer.runMonitorServer();
            result = "调用成功！";
        } catch (Exception e) {
            result = e.getCause() + e.getMessage();
        }
        return result;
    }

    @RequestMapping("/zr")
    public String testZR() {

        //Servers servers = ioc.getBean("servers", Servers.class);
        for (Infos infos : servers.infos) {
            System.out.println(infos.getIp());
        }

        System.out.println("servers:" + servers);
        return "";
    }



    //@RequestMapping("/t1")
    //@RequestMapping(value = "/t1",method = RequestMethod.GET)
    @GetMapping({"/t1"})
    public String t1() {
        return "调用成功测试！！！！";
    }

}
